﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Arkanoid
{
    class Ball
    {
        Texture2D texture;
        Vector2 position;
        public Vector2 Position 
        {
            get 
            {
                return position; 
            } 
            set 
            {
                position = value; 
            }
        }
        Rectangle size;
        Vector2 velocity;

        SoundEffect soundEffect;
        
        Random random = new Random();

        public Ball( 
            GameWindow janela ,
            Texture2D texture,
            Vector2 position,
            SoundEffect soundEffect)
        {
            this.texture = texture;
            this.size = texture.Bounds;
            this.position = new Vector2(
                janela.ClientBounds.Width/2
                - this.size.Width/2 ,
                janela.ClientBounds.Height/2
                - this.size.Height/2);

            float posicaoX = random.Next(-5,5);//TO DO evitar sortear 0
            float posicaoY = random.Next(-5,5);

            this.velocity = new Vector2(posicaoX, posicaoY);

            this.soundEffect = soundEffect;
        }
        
        public void Update(
             GameTime gameTime,
            GameWindow janela,
            Pallete playerL, Pallete playerR
           )
        {
            //position++;
            position += velocity;

            ManterNaTela(janela,playerL,playerR);
            
            //atualiza a posicao do rect da bola de acordo com  a sua posicao 
            this.size.X = (int)position.X;
            this.size.Y = (int)position.Y;

            if (playerL.size.Intersects(size))
            {
                velocity.X *= -1;
            }

            if (playerR.size.Intersects(size))
            {
                velocity.X *= -1;
            }
        }

        public void ManterNaTela(GameWindow janela, Pallete playerL, Pallete playerR)
        {
            //TO DO :
            //corrigir a colisao com a bola (e paleta) nos cantos
            //e aumentar a velocidade

            if (position.Y < 0 && velocity.Y < 0)
            {
                //velocity.Y++;
                velocity.Y = -velocity.Y;
                soundEffect.Play();
            }

            if (position.X < 0 && velocity.X<0)
            {
                //velocity.X++;
                velocity.X *= -1;
                playerR.points++;
                soundEffect.Play();
            }

            if (position.Y > janela.ClientBounds.Height - size.Height && velocity.Y > 0)
            {

                velocity.Y *= -1;
                soundEffect.Play();
            }

            if (position.X > janela.ClientBounds.Width - size.Width && velocity.X > 0)
            {
                velocity.X *= -1;
                playerL.points++;
                soundEffect.Play();
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);
        }        
    }
}
